package com.larry.tinyweb.core;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by larry on 16/12/16.
 *
 * @author larry
 */
public class MultipartRequest {
    private HttpServletRequest request;
    private String fileName;
    private File file;
    private Map<String, Object> param;
    private static String boundary = "--";

    public MultipartRequest(HttpServletRequest request) {
        this.request = request;
        param = new HashMap();
    }

    public void parse() {
        try {
//            System.out.println("ContentLength :" + request.getContentLength());

            ServletInputStream inputStream = request.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            loadPart(bufferedReader);
            if (inputStream != null) inputStream.close();
            /*String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadPart(BufferedReader bufferedReader) throws IOException {
        String line = null;
        while ((line = bufferedReader.readLine()) != null){
            if (line.endsWith(boundary)) {
                // 结束
                return;
            } else if (line.startsWith(boundary)) {
                // 本部分加载完成
                loadPart(bufferedReader);
            } else if(line.startsWith("Content-Disposition")) {
                line = line.substring("Content-Disposition: form-data;".length() + 1).trim();
//                System.out.println("- - - - - - - - - - -");
//                System.out.println(line);
                String contentType = bufferedReader.readLine();
                if (contentType != null && contentType.startsWith("Content-Type")) {
                    String[] ss = line.split(";");
                    for (String s : ss) {
                        String[] kv = s.trim().split("=");
                        param.put(kv[0].trim(), kv[1].trim().replaceAll("\"", ""));
                    }
                    // 读取二进制流
//                    System.out.println("读取二进制流");
                    String buffer = bufferedReader.readLine();
                    FileOutputStream fileOutputStream = new FileOutputStream(new File("./temp.png"));
                    do {
                        System.out.println(buffer);
                        fileOutputStream.write(buffer.getBytes());
                        buffer = bufferedReader.readLine();
                    } while (!buffer.startsWith(boundary));
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } else {
                    // 普通参数
//                    System.out.println("普通参数");
                    String[] kv = line.split("=");
                    String key = null;
                    if (kv.length == 2) key = kv[1].trim().replaceAll("\"", "");
                    line = bufferedReader.readLine();
                    this.param.put(key, line);
//                    System.out.println(key + " : " + line);
                }
            }
        }
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public Map<String, Object> getParam() {
        return param;
    }

    public void setParam(Map<String, Object> param) {
        this.param = param;
    }
}

class Part {

}
